// pages/ucenter/addLicensePlate/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addLicensePlates: [],
    showModal: false,
    items: [
      { value: 'aa', name: '浙K•R8229', type: '燃油汽车', checked: 'true' },
      { value: 'bb', name: '浙K•R8229', type: '新能源汽车', },
      { value: 'Cc', name: '浙K•R8229', type: '新能源汽车', },
      { value: 'dd', name: '浙K•R8229', type: '燃油汽车', }
    ]
    // par:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    app.getSetting(function () { 
      var userId = wx.getStorageSync('userId');
      that.addcar = that.selectComponent("#addcar");
      wx.request({
        //车辆查询
        url: app.globalData.basicUrl + 'user/userPlate/userId/' + userId,
        // data:1,
        success: function (res) {
          console.log(res);
          res.data.data.map(item => {
            if (item.carType == '1') {
              item.carType = '新能源汽车'
            } else {
              item.carType = '燃油汽车'
            }
          })
          that.setData({
            addLicensePlates: res.data.data
          })
        }
      })
    })
  },
  //添加车辆
  addcars: function (e) {
    this.addcar = this.selectComponent("#addcar");
    console.log(this.addcar)
    var that = this;
    var card = this.addcar.data.inputPlates
    var carType = this.addcar.data.flag ? '0' : '1'  //切换状态
    var pate = card.index0 + card.index1 + card.index2 + card.index3 + card.index4 + card.index5 + card.index6 + card.index7
    console.log("我输入的车牌号", pate, pate.length)
    let userId = wx.getStorageSync('userId')
    var par = {
      "userId": userId,
      "plateNo": pate,
      "carType": carType,
      "delFlag": "0"
    }
    var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
    var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    let result;
    if (pate.length == 7) {
      result = creg.test(pate);
    } else if (pate.length == 8) {
      result = xreg.test(pate);
    } else {
      result = false;
    }
    if (result) {
      wx.request({
        url: app.globalData.basicUrl + 'user/userPlate',
        method: "POST",
        data: par,
        success: function (res) {
          console.log("当前的res", res);
          //添加车辆
          //var msg = res.data.msg
          // wx.showToast({
          //   title: msg,
          // })
          if (res.data.code == 200) {
            wx.showToast({
              title: '添加成功',
              duration: 2000,
              success: function () {
                setTimeout(() => {
                  that.onReady();
                  that.addcar.reset()
                }, 2000)
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
              duration: 2000,
              success: function () {
                setTimeout(() => {
                  that.onReady();
                  that.addcar.reset()
                }, 2000)
              }
            })
          }
        },
        fail(res) {
          wx.showToast({
            icon: 'none',
            title: '添加失败',
            duration: 2000,
          })
        }
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '车牌号格式不正确',
        duration: 2000,
      })
    }

  },

  /**
   * 弹窗
   */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
    wx.showModal({
      title: '解绑成功',
      confirmText: "确定",
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
    console.log("解绑成功");
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
    wx.showModal({
      icon: "success",
      title: '您已取消操作',
      confirmText: "确定",
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
    console.log("您已取消操作");
  },



  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }

    this.setData({
      items
    })
  },



  // 车牌删除
  del: function (e) {
    var that = this;
    that.addcar = that.selectComponent("#addcar");
    console.log("获取当前的值", e);
    console.log(e.currentTarget.dataset.id);
    var plateId = e.currentTarget.dataset.id
    wx.showModal({
      title: '删除信息',
      content: '确定要解绑该车牌号吗？',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          console.log("取消");
          wx.showToast({
            title: '取消成功',
            icon: "none"
          })
        } else {
          //点击确定
          console.log("确定");
          wx.showToast({
            title: '删除成功',
            icon: "none"
          })
          wx.request({
            url: app.globalData.basicUrl + 'user/userPlate/' + plateId,
            method: "DELETE",
            success: function (res) {
              that.onReady();
              console.log(res);
            }
          })
        }
      },
      fail: function (res) { },//接口调用失败的回调函数
      complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})